package com.hr.mes.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hr.mes.dto.Page;
import com.hr.mes.entity.*;
import com.hr.mes.mapper.*;
import com.hr.mes.service.RoleService;
import com.hr.mes.utils.PageUtil;
import com.hr.mes.vo.AuthVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Transactional
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public void addRole(Role role) {
        roleMapper.addRole(role);
    }

    /**
     * 给角色授权
     * @param authVO
     */
    @CacheEvict(value = "shiro:auth", key = "#userId")
    @Override
    public void auth(AuthVO authVO) {
        rolePermissionMapper.deleteRolePermissionByRoleId(authVO.getRoleId());
        for (Integer permissionId : authVO.getPermissionIds()){
            rolePermissionMapper.addRolePermission(new RolePermission(authVO.getRoleId(), permissionId));
        }
    }

    @Override
    public void deleteRole(Integer roleId) {
        roleMapper.deleteRole(roleId);
        userRoleMapper.deleteUserRoleByRoleId(roleId);
        rolePermissionMapper.deleteRolePermissionByRoleId(roleId);
    }

    @Override
    public void updateRole(Role role) {
        roleMapper.updateRole(role);
    }

    @Override
    public Role getRole(Integer roleId) {
        Role role = roleMapper.getRole(roleId);
        List<RolePermission> rolePermissionList = rolePermissionMapper.getRolePermissionList(roleId);
        if(!CollectionUtils.isEmpty(rolePermissionList)){
            ArrayList<Permission> permissionList = new ArrayList<>();
            for (RolePermission rolePermission : rolePermissionList){
                Permission permission = permissionMapper.getPermission(rolePermission.getPermissionId());
                permissionList.add(permission);
            }
            role.setPermissionList(permissionList);
        }
        return role;
    }

    @Override
    public Page<Role> getRoleList(Integer pageNo, Integer pageSize) {
        List<Role> roleList = roleMapper.getRoleList();
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<Role> pageInfo = new PageInfo<>(roleList);
        Page<Role> page = PageUtil.ConvertPageInfoToPage(pageInfo);
        PageHelper.clearPage();
        return page;
    }

    @Override
    public List<Role> getAvailableRoleList() {
        List<Role> roleList = roleMapper.getRoleList();
        roleList = roleList.stream().filter(role -> role.getStatus() == 1).collect(Collectors.toList());
        return roleList;
    }

}
